import { _decorator, Component, director } from 'cc';
import { LayerRootAction } from './LayerRootAction';
import { Global } from './Global';
import { LayerCatAction } from './LayerCatAction';
import { LayerEmptyAction } from './LayerEmptyAction';
import { Clips, events } from './Enums';
import { AudioManger } from './AudioManger';
import { CatAction } from './CatAction';
import { MenuAction } from './MenuAction';
import { ShowLevelAction } from './ShowLevelAction';
import { GameOverAction } from './GameOverAction';
import { CoinsAction } from './CoinsAction';
import { SuccessAction } from './SuccessAction';
import { LayerBtnAction } from './LayerBtnAction';
import { TipsAction } from './TipsAction';
import { BucketAction } from './BucketAction';
import { PropsAction } from './PropsAction';
import { DjManger } from './DjManger';
import { GuideAction } from './GuideAction';
import { LvLStorage } from './LvLStorage';
import { Login } from './Login';
import { WxPlatform } from './ad/WxPlatform';

const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {

    @property({ type: LayerRootAction })
    public layer_root: LayerRootAction = null!;

    @property({ type: LayerCatAction })
    public layer_cat: LayerCatAction = null!;

    @property({ type: LayerEmptyAction })
    public layer_empty: LayerEmptyAction = null!;

    @property({ type: ShowLevelAction })
    public show_level_action: ShowLevelAction = null!;

    @property({ type: MenuAction })
    public menu: MenuAction = null!;

    @property({ type: GameOverAction })
    public game_over_action: GameOverAction = null!;

    @property({ type: CoinsAction })
    public coins_action: CoinsAction = null!;

    @property({ type: SuccessAction })
    public success_action: SuccessAction = null!;

    @property({ type: LayerBtnAction })
    public layer_btn_action: LayerBtnAction = null!;

    @property({ type: TipsAction })
    public tips_action: TipsAction = null!;

    @property({ type: BucketAction })
    public bucket_action: BucketAction = null!;

    @property({ type: PropsAction })
    public props_action: PropsAction = null!;

    @property({ type: GuideAction })
    public guide_action: GuideAction = null!;

    @property({ type: Number, tooltip: "基本扳手消耗，真实消耗是 lvl * need_coins" })
    public need_coins: number = 30;

    @property({ type: Login })
    public loginBtnNode: Login = null!;


    protected onLoad(): void {
        director.on(events.check_completed, this.nextLvlCheckCompleted, this);
    }

    protected onDestroy(): void {
        director.off(events.check_completed, this.nextLvlCheckCompleted, this);
    }


     start() {
        this.initGlobalReferences();
        this.restartGame();
    }


    private initGlobalReferences() {
        Global.Show_level_action = this.show_level_action;
        Global.need_coins = this.need_coins;
        Global.layer_root_action = this.layer_root;
        Global.layer_cat_action = this.layer_cat;
        Global.layer_empty_action = this.layer_empty;
        Global.main_action = this;
        Global.game_over_action = this.game_over_action;
        Global.coins_action = this.coins_action;
        Global.success_action = this.success_action;
        Global.layer_btn_action = this.layer_btn_action;
        Global.tips_action = this.tips_action;
        Global.bucket_action = this.bucket_action;
        Global.props_action = this.props_action;
        Global.guide_action = this.guide_action;
    }

    public restartGame() {
        const level = LvLStorage.get_cur_lvl();
        Global.restart_default(level);
        this.startLevel(level);
    }

    public startLevel(level: number) {
        Global.is_check_completed = true;
        LvLStorage.record_cur_lvl(level);
        Global.every_level_default();
        DjManger.reset_dj_cur_();
        console.log("开始游戏 level >>>>", level);
        this.layer_root.init_root(level);

        if (Global.Show_level_action) {
            Global.Show_level_action.show_level();
        } else {
            console.error("Show_level_action 未初始化，请检查组件绑定");
        }

        Global.coins_action.refresh_coins();
        Global.is_check_completed = false;
    }

    private nextLvlCheckCompleted(catAction: CatAction) {
        if (Global.cur_lvl_pin_move_num >= Global.cur_lvl_pin_total) {
            Global.success_action.open();
        } else {
            catAction.into_scence();
        }
    }

    public btnOpenMenu() {
        AudioManger.instance.playSound(Clips.btn_1);
        this.menu.open();
    }

    update(deltaTime: number) {
        // 游戏主循环逻辑（可扩展）
    }
}
